

import {FamilyTreeDrawerBase} from './family_tree_drawer_base'
import { Vector } from '../vector'
import {Person} from './person'
import {FamilyTreeData} from '../family_tree_data'
import {Line} from './line'
import {Couple} from './couple'
import {TreeSet} from './tree_set'
import {deepCopy} from '../util.js'

const app = getApp()

//画画布上的家族树
class FamilyTreeDrawer extends FamilyTreeDrawerBase {
    constructor(treeset_options = {}) {
      super()
      this.curr_family_id = null
      this.treeset_cache = {}  //family_id -> TreeSet
      this.treedata_cache = {}  //family_id -> FamilyTreeData
      /** {
      TreeSet绘制参数treeset_options:
      {
        can_select_person: true,   //是否可以选中人
        cancel_select_person: false,  //点击空白位置(人以外的位置)是否可取消选中人
        can_long_press_person: true  //是否支持长按人
      } */
      this.treeset_options = treeset_options  
    }

    SetContext(ctx, width, height) {
      //调用可能比SetData晚
      super.SetContext(ctx, width, height)
      this.InitTreesetPos()
    }

    //设置数据, 不影响当前treeset
    SetData(family_id, data) {
      console.log('begin set data')
      this.treedata_cache[family_id] = new FamilyTreeData()
      this.treedata_cache[family_id].SetData(data)
      let ts = TreeSet.CreateTreesetFromWithLevelData(this, this.treedata_cache[family_id].with_level_data, this.treeset_options)
      ts.family_id = family_id  //设置家族id缓存

      //如果没有初始位置，赋值初始位置
      let cache = this.cache
      if (ts.pos.IsZero()) {
        ts.Move(new Vector(cache.width/2, cache.height/10))
      }

      console.log('set treeset:', ts)
      this.treeset_cache[family_id] = ts
    }

    //获取家族整理后的数据
    GetWithLevelData(family_id) {
      if (family_id in this.treedata_cache) {
        return deepCopy(this.treedata_cache[family_id].with_level_data)
      } else {
        return null
      }
    }

    //获取当前家族整理后的数据
    GetCurrWithLevelData() {
      return this.GetWithLevelData(this.curr_family_id)
    }

    //切换家族
    SwitchFamily(family_id) {
      console.log('switch family: ', family_id)
      this.curr_family_id = family_id
      if (family_id in this.treeset_cache) {
        this.InitTreesetPos()
      } else {
        //请求后台获取数据
        app.httpRequest('select_family', {family_id: family_id}, res => {
          console.log('select_family: ', res)
          this.SetData(family_id, res)
          if (family_id in this.treeset_cache) {
            this.InitTreesetPos()
          }
        }, res => {
          console.log('select_family failed: ', res)

        })
      }
    }

    //鼠标选中人回调
    SelectPersonCallback(personId) {
      if (this.select_person_callback) {
        this.select_person_callback(personId)
      }
    }

    //注册鼠标选中回调, 回调接收一个personId参数
    RegisterSelectPersonCallback(cb) {
      this.select_person_callback = cb
    }

    //设置当前家族的初始位置
    InitTreesetPos() {
      let cache = this.cache
      cache.offset = new Vector(0, 0)  //当前偏移
      cache.scale = 1   //放大参数
      if (this.curr_family_id in this.treeset_cache) {
        let curr_treeset = this.treeset_cache[this.curr_family_id]
        if (curr_treeset.pos.IsZero()) {
          curr_treeset.Move(new Vector(cache.width/2, cache.height/10))
        }
      }
    }

    //一次整体绘制
    Render() {
      let cache = this.cache
      this.ctx.clearRect(0, 0, cache.width, cache.height)  //刷新整个页面

      if (this.curr_family_id in this.treeset_cache) {
        let curr_treeset = this.treeset_cache[this.curr_family_id]
        curr_treeset.Draw()
      }
    }

    //只清理，啥也不做
    RenderClear() {
      let cache = this.cache
      this.ctx.clearRect(0, 0, cache.width, cache.height)  //刷新整个页面
    }

    //单指在某个位置按下的回调
    TapDownCallback(pos) {
      console.log("tap down: ", pos)
      if (this.curr_family_id in this.treeset_cache) {
        let curr_treeset = this.treeset_cache[this.curr_family_id]
        curr_treeset.TapDown(pos)
      }
    }

    //单指按下移动
    TapMoveCallback(pos) {
      if (this.curr_family_id in this.treeset_cache) {
        let curr_treeset = this.treeset_cache[this.curr_family_id]
        curr_treeset.TapMove(pos)
      }
    }

    //单指松开的回调
    TapUpCallback(pos) {
      console.log("tap up: ", pos)
      if (this.curr_family_id in this.treeset_cache) {
        let curr_treeset = this.treeset_cache[this.curr_family_id]
        curr_treeset.TapUp(pos)
      }
    }

    //双指开始回调
    DoubleTapStartCallback() {
      if (this.curr_family_id in this.treeset_cache) {
        let curr_treeset = this.treeset_cache[this.curr_family_id]
        curr_treeset.DoubleTapStart()
      }
    }

    //删除tap缓存
    ClearTapCache() {
      if (this.curr_family_id in this.treeset_cache) {
        let curr_treeset = this.treeset_cache[this.curr_family_id]
        curr_treeset.ClearTap()
      }
    }
}

module.exports = {
  FamilyTreeDrawer
}